/*
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <fcntl.h>
#include <cstdio>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include "It_process_plimits.h"
#include <mqueue.h>

#define MQ_OPEN_COUNT_MAX 3

const int MQUEUE_STANDARD_NAME_LENGTH  = 50;

mqd_t g_mqueue_015[MQ_OPEN_COUNT_MAX];

CHAR g_mqname_015[MQUEUE_STANDARD_NAME_LENGTH] = "";

std::string g_subPlimitsPath = "/proc/plimits/test";
std::string g_configFileProcs = "/proc/plimits/test/plimits.procs";
std::string g_configFileMqCount = "/proc/plimits/test/ipc.mq_count";
std::string g_mqLimitCount = "3";
struct mq_attr g_attr_015 = { 0 };
extern UINT32 LosCurTaskIDGet();
char buf_0[512] = { 0 };
char *array_0[4] = { 0 };
char buf_1[512] = { 0 };
char *array_1[4] = { 0 };    
int mqSuccessCount_0 = -1, mqFailedCount_0 = -1;
int mqSuccessCount_1 = -1, mqFailedCount_1 = -1;
int g_index;

int Fork_015()
{
    pid_t pid;
    int ret;

    char pidStr[3] = { 0 };
    g_attr_015.mq_maxmsg = 1;
    pid = fork();
    sprintf_s(pidStr, sizeof(pidStr), "%d", getpid());
    if(pid < 0) {
        return 0;
    } else if (pid == 0) {
        ret = WriteFile(g_configFileProcs.c_str(), pidStr);
        ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
        for (g_index = 0; g_index < MQ_OPEN_COUNT_MAX; ++g_index) {
            snprintf(g_mqname_015, MQUEUE_STANDARD_NAME_LENGTH, "/mq_015_1_%d_%d", g_index, LosCurTaskIDGet());
            g_mqueue_015[g_index] = mq_open(g_mqname_015, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &g_attr_015);
            ICUNIT_GOTO_NOT_EQUAL(g_mqueue_015[g_index], (mqd_t)-1, g_mqueue_015[g_index], FORK_EXIT);
        }
        snprintf(g_mqname_015, MQUEUE_STANDARD_NAME_LENGTH, "/mq_015_1_%d_%d", g_index, LosCurTaskIDGet());
        g_mqueue_015[g_index] = mq_open(g_mqname_015, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &g_attr_015);
        ICUNIT_GOTO_EQUAL(g_mqueue_015[g_index], (mqd_t)-1, g_mqueue_015[g_index], FORK_EXIT);

        ret = ReadFile("/proc/plimits/test/ipc.stat", buf_0);
        GetLine(buf_0, 4, 254, array_0);
        mqSuccessCount_0 = atoi(array_0[0] + strlen("mq success count: "));
        mqFailedCount_0 = atoi(array_0[1] + strlen("mq failed count: "));
        exit(0);
    } else if (pid > 0) {
        return 0;
    }
FORK_EXIT:
    for (int k = 0; k < g_index; ++k) {
        ret = mq_close(g_mqueue_015[k]);
        snprintf(g_mqname_015, MQUEUE_STANDARD_NAME_LENGTH, "/mq_015_1_%d_%d", k, LosCurTaskIDGet());
        ret = mq_unlink(g_mqname_015);
        ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    }
    exit(0);
}

// plimits_ipc_19
static int Testcase(VOID)
{
    mode_t mode;
    int ret;
    int status;
    pid_t wpid;
    char pidStr[3] = { 0 };
    sprintf_s(pidStr, sizeof(pidStr), "%d", getpid());
    g_attr_015.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH;
    g_attr_015.mq_maxmsg = 1;

    if (access(g_subPlimitsPath.c_str(), F_OK) == -1) {
        ret = mkdir(g_subPlimitsPath.c_str(), S_IFDIR | mode);
        ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    }

    ret = WriteFile(g_configFileMqCount.c_str(), g_mqLimitCount.c_str());
    ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, EXIT_0);

    Fork_015();

    wpid = wait(&status);

    sleep(1);
    ret = ReadFile("/proc/plimits/test/ipc.stat", buf_1);
    GetLine(buf_1, 4, 254, array_1);
    mqSuccessCount_1 = atoi(array_1[0] + strlen("mq success count: "));
    mqFailedCount_1 = atoi(array_1[1] + strlen("mq failed count: "));
    
    goto EXIT_1;

EXIT_1:
    for (int k = 0; k < g_index; ++k) {
        ret = mq_close(g_mqueue_015[k]);
        snprintf(g_mqname_015, MQUEUE_STANDARD_NAME_LENGTH, "/mq_015_1_%d_%d", k, LosCurTaskIDGet());
        ret = mq_unlink(g_mqname_015);
    }
    goto EXIT_0;

EXIT_0:
    RmdirControlFile(g_subPlimitsPath);
    ret = rmdir(g_subPlimitsPath.c_str());
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    return 0;

}

void ItProcessPlimitsIpc015(void)
{
    TEST_ADD_CASE("IT_PROCESS_PLIMITS_IPC_015", Testcase, TEST_PROCESS, TEST_PLIMITS, TEST_LEVEL0, TEST_FUNCTION);
}